package com.ayu.wxgzh.ayucyy.chain.handler;

import com.ayu.wxgzh.ayucyy.entity.Article;
import com.ayu.wxgzh.ayucyy.entity.BaseMessage;
import com.ayu.wxgzh.ayucyy.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author 徐林玉
 * @Description NewsRandomHandler
 * @Date 2022/9/19 9:27
 * @Version 1.0
 */
@Component
@Slf4j
public class NewsRandomHandler extends AbstractHandler {
    @Override
    public BaseMessage handle(Map<String, String> requestMap) {

        //降级：如果配置了降级，则跳过此处理器，执行下一个处理器
        if (super.getConfig().getDown()) {
            log.warn("新闻推送 NewsRandomHandler 已降级，跳过新闻推送 NewsRandomHandler...");
            return super.next(requestMap);
        }

        // 用户输入图文时回复一个图文消息
        String content = requestMap.get("Content");
        if (StringUtils.equals("新闻", content)) {

            Article article = NewsService.getRandomNews();

            BaseMessage articleMessage = getArticleMessage(article, requestMap);

            return articleMessage;
        }


        return super.next(requestMap);
    }
}
